<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-descriptions :column="2" border >
            <el-descriptions-item :rowspan="2">
                <template #label>
                    <div class="cell-item">合理化建议</div>
                </template>
                <div style="width: 200px;"><el-text>{{ ruleForm.content }}</el-text></div>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">建议人</div>
                </template>
                <div style="width: 200px;"><el-text>{{ user.name }}</el-text></div>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">日期</div>
                </template>
                <div style="width: 200px;"><el-text>{{ _date }}</el-text></div>
            </el-descriptions-item>
        </el-descriptions>
    </el-form>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-descriptions :column="2" border >
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">是否被采纳</div>
                </template>
                <div style="width: 200px;">
                    <el-radio-group v-model="ruleForm.status">
                        <el-radio :value="1" >是</el-radio>
                        <el-radio :value="0" >否</el-radio>
                    </el-radio-group>
                </div>
            </el-descriptions-item>
            <el-descriptions-item >
                <template #label>
                    <div class="cell-item">奖励　</div>
                </template>
                <div style="width: 200px;"><el-input v-model="ruleForm.reward" type="textarea" :autosize="{ minRows: 4 }"  /></div>
            </el-descriptions-item>
        </el-descriptions>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../util/request';
let user:any = sessionStorage.getItem('user')
user = JSON.parse(user)
const _date = ref<any>('')
const loading = ref<boolean>(false)
interface RuleForm {
    id: number
    created_at:string
    user_id:number
    content:string
    status:number
    reward:string
    
}

const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    created_at:'',
    user_id:0,
    status:0,
    content:'/',
    reward:'/',
})

const onGetData = () => {
    const date = new Date();
    const isoString = date.toISOString(); // 输出：2023-10-05T14:30:00.000Z
    _date.value = isoString.split('T')[0]; // 截取日期部分
    if(!ruleForm.value.id){
        return
    }
    http.get('/suggest/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
            _date.value = ruleForm.value.created_at
        }
        
    })
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    content: [
        { required: true, message: '请输入合理化建议', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            http.patch('/suggest/status',ruleForm.value).then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
.el-tab-pane{height: calc(100vh - 25vh);overflow-y: scroll;}
::v-deep .el-form-item .el-form-item__label {
  font-size: 16px; /* 或者你想要的任何大小 */
}
::v-deep .el-tabs__item{font-size: 18px !important;}
</style>
